﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ERPStore.Cart.Repositories
{
	public static class QueryExtensions
	{
		public static IQueryable<Datas.Cart> Current(this IQueryable<Datas.Cart> query)
		{
			return from cart in query
					where cart.CartStateId == (int)ERPStore.Models.CartState.CurrentOrder
					select cart;
		}

		public static Datas.Cart GetById(this IQueryable<Datas.Cart> query, int id)
		{
			return query.Where(i => i.Id == id).FirstOrDefault();
		}

		public static IQueryable<Datas.Cart> GetByUserPrincipal(this IQueryable<Datas.Cart> query, ERPStore.Models.UserPrincipal principal)
		{
			return from cart in query
				   where cart.VisitorId == principal.VisitorId
				   select cart;
		}

		public static IQueryable<Datas.Cart> GetByVisitorId(this IQueryable<Datas.Cart> query, string visitorId)
		{
			return from cart in query
				   where cart.VisitorId == visitorId
				   select cart;
		}

		public static IQueryable<Datas.Cart> GetActive(this IQueryable<Datas.Cart> query)
		{
			return from cart in query
				   where cart.CartStateId == (int)ERPStore.Models.CartState.CurrentOrder
					|| cart.CartStateId == (int)ERPStore.Models.CartState.Order
				   select cart;
		}

		public static Datas.Cart GetByCode(this IQueryable<Datas.Cart> query, string code)
		{
			query = query.Where(i => i.Code == code);
			return query.FirstOrDefault();
		}

		public static Datas.Cart GetByConvertedEntityId(this IQueryable<Datas.Cart> query, int convertedEntityId)
		{
			return query.Where(i => i.ConvertedEntityId.HasValue && i.ConvertedEntityId.Value == convertedEntityId).FirstOrDefault();
		}
	}
}
